package com.orifine.web.validate;

import com.orifine.web.Domain;
import com.orifine.web.Property;
import com.orifine.web.Validator;


public class StringLengthRule {
	public static Validator min(final int minLength){
		return new Validator() {
			@Override
			public boolean execute(Object... values) {
				String value = (String) values[0];
				int length = 0;
				if(value != null){
					length = value.toString().length();
				}
				return length >= minLength;
			}

			@Override
			public Property property() {
				return new Property(new Domain("validate.string.length.min"), getDomain(), minLength);
			}
		};
	}
	
	public static Validator max(final int maxLength){
		return new Validator() {
			@Override
			public boolean execute(Object... values) {
				String value = (String) values[0];
				int length = 0;
				if(value != null){
					length = value.toString().length();
				}
				return length <= maxLength;
			}

			@Override
			public Property property() {
				return new Property(new Domain("validate.string.length.max"), getDomain(), maxLength);
			}
		};
	}
	
	public static Validator range(final int minLength, final int maxLength){
		return new Validator() {
			@Override
			public boolean execute(Object... values) {
				String value = (String) values[0];
				int length = 0;
				if(value != null){
					length = value.toString().length();
				}
				return length >= minLength && length <= maxLength;
			}

			@Override
			public Property property() {
				return new Property(new Domain("validate.string.length.range"), getDomain(), minLength, maxLength);
			}
		};
	}
}
